package com.hph.user.service;

import com.hph.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

/**
 * @author {USER}
 * @date {date}
 */
@Service
public class UserServiceProxy implements UserService{

    /**
     * 这里不需要IP，Eureka可以帮我们转换
     */
    private final static String PROVIDER_SERVER_URL_PREFIX = "http://user-service-provider/";

    /**
     * 通过REST API代理到服务提供者
     */
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public Boolean save(User user) {

        User returnValue = restTemplate.postForObject(PROVIDER_SERVER_URL_PREFIX+"/user/save", user.getName(), User.class);

        return returnValue != null;
    }

    @Override
    public Collection<User> listAll() {

        return restTemplate.getForObject(PROVIDER_SERVER_URL_PREFIX+"/user/list", Collection.class);
    }
}
